package com.sajus.web.billing.admin.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sajus.billing.client.delegate.BusinessDelegateFactory;
import com.sajus.billing.client.dto.ServiceTypeDTO;

/**
 * Acts as a Struts 2 controller that responds
 * to a user action by setting the value
 * of the Message model class, and returns a String 
 * result.
 * @author Bruce Phillips
 *
 */
public class ServiceAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * The model class that stores the message
	 * to display in the view.
	 */
	private List<ServiceTypeDTO> serviceTypes;
	
	private ServiceTypeDTO serviceType;
	
	/*
	 * Creates the MessageStore model object and 
	 * returns success.  The MessageStore model
	 * object will be available to the view.
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		
		if ( this.serviceType != null ) {
			if ( this.serviceType.getId() == null ){
				if ( this.serviceType.getDescription() != null ||
						this.serviceType.getServicecolumn() != null ||
						this.serviceType.getServicetable() != null ||
						this.serviceType.getType() != null ||
						this.serviceType.getUnit() != null ){
					BusinessDelegateFactory.getInstance().getServiceTypeManager().saveServiceType( this.serviceType );
					System.out.println("Persist object Success reset value" );
					this.serviceType = new ServiceTypeDTO();
				}
			} else {
				BusinessDelegateFactory.getInstance().getServiceTypeManager().saveServiceType( this.serviceType );
				System.out.println("Persist object Success reset value" );
				this.serviceType = new ServiceTypeDTO();
			}
		}
		
		String paramValue = ServletActionContext.getRequest().getParameter("serviceTypeId");
		if ( paramValue != null && !paramValue.equals( "" ) ){
			Integer id = new Integer( Integer.parseInt( paramValue ) );
		
			this.serviceType = BusinessDelegateFactory.getInstance().getServiceTypeManager().getServiceType( id );
		}
		if( this.serviceTypes == null ){
			serviceTypes = BusinessDelegateFactory.getInstance().
					getServiceTypeManager().getServiceTypes();
		}
		
		return SUCCESS;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public List<ServiceTypeDTO> getServiceTypes() {
		return serviceTypes;
	}


	public void setServiceTypes(List<ServiceTypeDTO> serviceTypes) {
		this.serviceTypes = serviceTypes;
	}


	public ServiceTypeDTO getServiceType() {
		return serviceType;
	}


	public void setServiceType(ServiceTypeDTO serviceType) {
		this.serviceType = serviceType;
	}

}
